package com.qt.map;

import com.qt.map.entity.Student;

import java.util.*;
import java.util.function.BiConsumer;

public class HashMapDemo {
    public static void main(String[] args) {

        HashMap<String,String> map = new HashMap<>();
        map.put("1","1");
        map.put("2","2");

        LinkedHashMap<String, String> linkedMap = new LinkedHashMap<>();


        System.out.println("-----------");

        new HashMapDemo().test1();

        new  HashMapDemo().test2();



    }

    /*
    需求
        创建一个HashMap集合，键是学生对象(Student)，值是籍贯(String).
        存储三个键值对元素，并遍历
        要求:同姓名，同年龄认为是同一个学生
     */
    public void test1(){
        HashMap<Student, String> strMap = new HashMap<>();
        Student s1 = new Student("张三",18);
        Student s2 = new Student("李四",18);
        Student s3 = new Student("王五",18);
        Student s4 = new Student("李四",18);
        strMap.put(s1,"北京");
        strMap.put(s2,"上海");
        strMap.put(s3,"西安");
        strMap.put(s4,"河南");
        strMap.forEach(( student,  s)-> System.out.println(student.toString()+"-->"+s));

    }


    /*
    需求
    某个班级80名学生，现在需要组成秋游活动，班长提供了四个景点依次是(A、B、C、D),每个学生只
    能选择一个景点，请统计出最终哪个景点想去的人数最多。

     */

    public void test2(){
        String[] arr = {"A","B","C","D"};
        Random random = new Random();
        HashMap<String, Integer> strMap = new HashMap<>();
        for (int i = 0; i < 80; i++) {
            int index = random.nextInt(arr.length);
            String s = arr[index];
            if(strMap.containsKey(s)){
                strMap.put(s,strMap.get(s)+1);
            }else {
                strMap.put(s,1);
            }
        }
        strMap.forEach((key,value)-> System.out.println(key + "-->" + value));


        TreeMap<String, String> strTreeMap = new TreeMap<>();


    }

}
